(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(['controllers/base/controller', 'views/user/login_view', 'views/user/index_view', 'views/user/form_view', 'models/user_model', 'collections/user_collection', 'daos/access_dao', 'daos/user_dao', 'lib/dao_factory'], function() {
    'use strict';
    var AccessDAO, Controller, DaoFactory, FormView, IndexView, LoginView, UserCollection, UserController, UserDAO, UserModel, _ref;
    Controller = require('controllers/base/controller');
    UserModel = require('models/user_model');
    UserCollection = require('collections/user_collection');
    LoginView = require('views/user/login_view');
    DaoFactory = require('lib/dao_factory');
    IndexView = require('views/user/index_view');
    FormView = require('views/user/form_view');
    AccessDAO = DaoFactory.getDAOByName('access_dao');
    UserDAO = DaoFactory.getDAOByName('user_dao');
    return UserController = (function(_super) {
      __extends(UserController, _super);

      function UserController() {
        _ref = UserController.__super__.constructor.apply(this, arguments);
        return _ref;
      }

      UserController.prototype.beforeAction = function() {
        UserController.__super__.beforeAction.apply(this, arguments);
        return this.model = new UserModel;
      };

      UserController.prototype.index = function() {
        console.log(this.globals.userData);
        if (this.globals.userData.tipoUsuario === 'ADMINISTRADOR') {
          this.collection = new UserCollection;
          return this.view = new IndexView({
            collection: this.collection
          });
        } else {
          return this.redirectTo({
            url: 'usuario/' + this.globals.userData.id
          });
        }
      };

      UserController.prototype.login = function(params) {
        var _signin,
          _this = this;
        this.view = new LoginView({
          model: this.model
        });
        _signin = function(formData) {
          var promise;
          if (!formData.username && !formData.password) {
            return;
          }
          _this.model.set('username', formData.username);
          promise = AccessDAO.login(formData.username, formData.password);
          promise.done(function() {
            return _this.redirectTo({
              url: 'usuario/index'
            });
          });
          return promise.fail(function(response) {
            _this.publishEvent('feedback:error', 'Usuário e(ou) senha inválido(s)');
            return _this.view.enableActions();
          });
        };
        return this.view.on('signin', _signin);
      };

      UserController.prototype.logout = function() {
        AccessDAO.logout();
        return this.redirectTo({
          url: ''
        });
      };

      UserController.prototype.create = function() {
        this.model = new UserModel;
        this.view = new FormView({
          model: this.model,
          isNovoUsuario: false
        });
        return this._setupHistoryBack();
      };

      UserController.prototype.novo = function() {
        this.model = new UserModel;
        this.view = new FormView({
          model: this.model,
          isNovoUsuario: true
        });
        return this._setupHistoryBack();
      };

      UserController.prototype.edit = function(params) {
        this.model = new UserModel({
          id: params.id
        });
        this.view = new FormView({
          model: this.model
        });
        return this._setupHistoryBack();
      };

      UserController.prototype._setupHistoryBack = function() {
        var _this = this;
        return this.view.on('model:saved', function() {
          _this.publishEvent('feedback:success', 'Usuário salvo com sucesso');
          return _this.redirectTo({
            url: 'usuario'
          });
        });
      };

      return UserController;

    })(Controller);
  });

}).call(this);
